<!DOCTYPE html>
<html lang="ZH-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>登录</title>
    <link rel="stylesheet" href="./plugin/layui/css/layui.css">
    <link rel="stylesheet" href="./src/lvdong/css/login.css">
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <script src="https://lib.baomitu.com/axios/0.19.2/axios.js"></script>
</head>

<body>
<div class="kit-login" id="app">
    <div class="kit-login-bg"></div>
    <div class="kit-login-wapper">
        <h2 class="kit-login-slogan">欢迎使用 <br> 巴中市教务管理系统</h2>
        <div class="kit-login-form">
            <h4 class="kit-login-title">登录</h4>
            <form class="layui-form">
                <div class="kit-login-row">
                    <div class="kit-login-col">
                        <i class="layui-icon">&#xe612;</i>
                        <span class="kit-login-input">
                <input type="text" v-model="uname" placeholder="用户名/手机号"/>
              </span>
                    </div>
                    <div class="kit-login-col"></div>
                </div>
                <div class="kit-login-row">
                    <div class="kit-login-col">
                        <i class="layui-icon">&#xe64c;</i>
                        <span class="kit-login-input">
                <input type="password" v-model="pwd" placeholder="密码"/>
              </span>
                    </div>
                    <div class="kit-login-col"></div>
                </div>
                <div class="kit-login-row">
                    <div class="kit-login-col">
                        <input type="checkbox" name="rememberMe" title="记住帐号" lay-skin="primary">
                    </div>
                </div>
                <div class="kit-login-row">
                    <button class="layui-btn kit-login-btn" @click="login()" type="button">登录</button>
                </div>
                <div class="kit-login-row" style="margin-bottom:0;">
                    <a href="javascript:;" style="color: rgb(153, 153, 153); text-decoration: none; font-size: 13px;"
                       id="forgot">忘记密码</a>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    new Vue({
        el: "#app",
        data: {
            uname: "",
            pwd: ""
        },
        methods: {
            login: function () {
                var params = new URLSearchParams();
                params.append("account", this.uname);
                params.append("pwd", this.pwd);
                axios.post("/auth/login", params)
                    .then(res => {
                        console.log(res.data);
                        if (res.data.code == 200) {
                            //在当前浏览器所有页面都可以获取storage;
                            var storage = window.localStorage;
                            //清空storage中的数据
                            storage.clear();
                            //存放数据
                            storage.setItem("token", res.data.token);
                            storage.setItem("refreshToken", res.data.refreshToken);
                            if(res.data.role == "principal"){
                                window.location.href = "index.html";
                            }else if(res.data.role == "teacher"){
                                window.location.href = "choose.html";
                            }else if(res.data.role == "admin"){
                                window.location.href = "edubureau/index.html";
                            }else if(res.data.role == "student"){
                                window.location.href = "student/zlz/index.html";
                            }
                        }else if (res.data.code == 201){
                            alert("账号不存在")
                        }else if (res.data.code == 202){
                            alert("密码错误")
                        }
                    })
            }
        }

    })
</script>
</body>

</html>